package com.ss.android.article.base.ui;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.browser.R;

public class DiggLayout extends ViewGroup {
    private boolean clicked;
    private String B;
    private Paint.FontMetrics C;
    public AnimationImageView mAnimationImageView;
    public boolean b;
    private int c;
    private DiggAnimationView diggAnimationView;
    private Paint e;
    private float f;
    private float g;
    private int h;
    private int i;
    private int j;
    private float k;
    private float l;
    private float m;
    private float n;
    private int o;
    private int p;
    private int q;
    private int r;
    private int s;
    private int t;
    private ViewGroup.LayoutParams u;
    private int v;
    private int w;
    private int x;
    private Resources mResources;
    private DisplayMetrics mDisplayMetrics;

    public DiggLayout(Context arg2) {
        this(arg2, null);
    }

    public DiggLayout(Context arg2, AttributeSet arg3) {
        this(arg2, arg3, 0);
    }

    public DiggLayout(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        float v8 = 1f;
        this.t = 2;
        this.x = 2;
        this.mDisplayMetrics = new DisplayMetrics();
        this.B = "";
        this.mResources = context.getResources();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(this.mDisplayMetrics);
        this.b = com.ss.android.j.b.isNightModeToggled();
        this.mAnimationImageView = new AnimationImageView(context);
        this.setClipToPadding(false);
        this.setClipChildren(false);
        this.setWillNotDraw(false);
        this.mAnimationImageView.a(com.ss.android.article.browser.R.drawable.digup_video_pressed, R.drawable.digup_video_normal, this.b);
        this.e = new Paint(1);
        if (attributeSet != null) {
            TypedArray v0 = context.obtainStyledAttributes(attributeSet, com.ss.android.article.base.a.DiggLayout, defaultStyle, 0);
            this.x = v0.getInt(com.ss.android.article.base.a.v, 2);
            switch (this.x) {
                case 1: {
                    this.g = TypedValue.applyDimension(2, 10f, this.mDisplayMetrics);
                    this.f = TypedValue.applyDimension(1, v8, this.mDisplayMetrics);
                    break;
                }
                case 2: {
                    this.g = TypedValue.applyDimension(2, 12f, this.mDisplayMetrics);
                    this.f = TypedValue.applyDimension(1, 4f, this.mDisplayMetrics);
                    break;
                }
            }

            this.t = v0.getInt(com.ss.android.article.base.a.o, 2);
            this.o = v0.getInt(com.ss.android.article.base.a.u, 2);
            this.r = ((int) v0.getDimension(com.ss.android.article.base.a.t, 0f));
            this.s = ((int) v0.getDimension(com.ss.android.article.base.a.s, 0f));
            this.v = v0.getResourceId(com.ss.android.article.base.a.n, 0);
            this.c = v0.getInt(com.ss.android.article.base.a.r, 0);
            this.g = v0.getDimension(com.ss.android.article.base.a.q, this.g);
            this.f = v0.getDimension(com.ss.android.article.base.a.p, this.f);
            v0.recycle();
        } else {
            switch (this.x) {
                case 1: {
                    this.g = TypedValue.applyDimension(2, 10f, this.mDisplayMetrics);
                    this.f = TypedValue.applyDimension(1, v8, this.mDisplayMetrics);
                    break;
                }
                case 2: {
                    this.g = TypedValue.applyDimension(2, 12f, this.mDisplayMetrics);
                    this.f = TypedValue.applyDimension(1, 4f, this.mDisplayMetrics);
                    break;
                }
            }
        }
        this.u = new ViewGroup.LayoutParams(-2, -2);
        this.addView(this.mAnimationImageView, this.u);
        this.mAnimationImageView.measure(View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        this.e.setTextSize(this.g);
        this.C = this.e.getFontMetrics();
        this.i = R.color.ssxinzi4;
        this.j = R.color.ssxinzi3;
        this.adaptNightMode(this.b);
    }

    public final void onDiggClick() {
        if (!this.mAnimationImageView.d
                && mAnimationImageView != null
                && !this.clicked) {
            this.mAnimationImageView.a();
            if (this.diggAnimationView == null || this.clicked) {
                Logger.d("DiggLayout", "onDiggClick, diggAnimationView is null");
            } else {
                int v5 = ((int) TypedValue.applyDimension(1, 4f, diggAnimationView.c));
                int v6 = ((int) TypedValue.applyDimension(1, 2f, diggAnimationView.c));
                int[] distance = new int[2];
                UIUtils.getDistance(mAnimationImageView, diggAnimationView, distance);
                ViewGroup.LayoutParams layoutParams = diggAnimationView.getLayoutParams();
                if ((layoutParams instanceof ViewGroup.MarginLayoutParams)) {
                    ((ViewGroup.MarginLayoutParams) layoutParams).leftMargin = v5 -distance[0] + (mAnimationImageView.getWidth() - DiggAnimationView.d) / 2 + ((MarginLayoutParams) layoutParams).leftMargin;
                    ((ViewGroup.MarginLayoutParams) layoutParams).topMargin = mAnimationImageView.getPaddingTop() + (-distance[1] - com.ss.android.article.base.ui.DiggAnimationView.e - v6) + ((ViewGroup.MarginLayoutParams) layoutParams).topMargin;
                    diggAnimationView.setLayoutParams(layoutParams);
                    diggAnimationView.startAnimation(diggAnimationView.mScaleAnimation);
                }
            }

            this.clicked = !this.clicked;
            this.adaptNightMode(this.b);
        }
    }

    public final void adaptNightMode(boolean isNightMode) {
        this.b = isNightMode;
        int v0 = this.clicked ? this.i : this.j;
        this.h = v0;
        this.e.setColor(this.mResources.getColor(this.h));
        if (this.v > 0) {
            this.w = this.v;
            if (this.w == this.v) {
                this.setBackgroundDrawable(this.getResources().getDrawable(this.w));
            } else {
                this.setBackgroundResource(this.w);
            }
        }

        this.mAnimationImageView.adaptNightMode(isNightMode);
        if (this.diggAnimationView != null) {
            this.diggAnimationView.a();
        }

        this.invalidate();
    }

    public final void a(int arg4, int arg5, boolean arg6) {
        if (this.mAnimationImageView != null) {
            this.mAnimationImageView.a(arg4, arg5, arg6);
            this.mAnimationImageView.measure(View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            this.requestLayout();
        }
    }

    protected void onDraw(Canvas arg5) {
        if (!TextUtils.isEmpty(this.B)) {
            arg5.drawText(this.B, this.m, this.n, this.e);
        }
    }

    protected void onLayout(boolean arg9, int arg10, int arg11, int arg12, int arg13) {
        int v2;
        int v0;
        int v4 = 2;
        int v1 = 0;
        float v7 = 2f;
        this.q = arg13 - arg11;
        this.p = arg12 - arg10;
        if (this.c == 0) {
            v0 = this.getPaddingLeft();
            switch (this.t) {
                case 1: {
                    v1 = this.getPaddingTop();
                    break;
                }
                case 2: {
                    v1 = this.getPaddingTop() + (this.q - this.getPaddingBottom() - this.getPaddingTop() - this.mAnimationImageView.getMeasuredHeight()) / 2;
                    break;
                }
                case 3: {
                    v1 = this.q - this.getPaddingBottom() - this.mAnimationImageView.getMeasuredHeight();
                    break;
                }
                case 4: {
                    v0 = (this.p - this.getPaddingLeft() - this.getPaddingRight() - (((int) ((((float) this.mAnimationImageView.getMeasuredWidth())) + this.f + this.k)))) / 2 + this.getPaddingLeft();
                    v1 = this.getPaddingTop() + (this.q - this.getPaddingBottom() - this.getPaddingTop() - this.mAnimationImageView.getMeasuredHeight()) / 2;
                    break;
                }
            }

            this.mAnimationImageView.layout(v0, v1, this.mAnimationImageView.getMeasuredWidth() + v0, this.mAnimationImageView.getMeasuredHeight() + v1);
            switch (this.o) {
                case 2: {
                    this.n = (((float) (v1 + this.mAnimationImageView.getMeasuredHeight() / 2))) - (this.C.ascent + this.C.descent) / v7 + v7;
                    break;
                }
                case 3: {
                    v2 = UIUtils.getDensityDpi(this.getContext()) >= 480 ? 6 : 4;
                    this.n = ((float) (v1 + this.mAnimationImageView.getMeasuredHeight() - v2));
                    break;
                }
                default: {
                    this.n = (((float) (v1 + this.mAnimationImageView.getMeasuredHeight() / 2))) - (this.C.ascent + this.C.descent) / v7 + v7;
                    break;
                }
            }

            this.m = (((float) (v0 + this.mAnimationImageView.getMeasuredWidth()))) + this.f;
        } else {
            if (this.c != v4 && this.c != 3) {
                if (this.c == 1) {
                    this.m = ((((float) this.p)) - this.k - this.f - (((float) this.mAnimationImageView.getMeasuredWidth()))) / v7;
                    v0 = Math.max(((int) this.l), this.mAnimationImageView.getMeasuredHeight());
                    switch (this.t) {
                        case 1: {
                            v1 = this.getPaddingTop();
                            break;
                        }
                        case 3: {
                            v1 = this.q - this.getPaddingBottom() - v0;
                            break;
                        }
                        case 2:
                        case 4: {
                            v1 = (this.q - v0) / 2;
                            break;
                        }
                    }

                    v2 = ((int) (this.m + this.k + this.f));
                    int v3 = (v0 - this.mAnimationImageView.getMeasuredHeight()) / 2 + v1;
                    this.mAnimationImageView.layout(v2, v3, this.mAnimationImageView.getMeasuredWidth() + v2, this.mAnimationImageView.getMeasuredWidth() + v3);
                    this.n = ((((float) v0)) - this.l) / v7 + (((float) v1)) - this.C.ascent;
                } else {
                }

                return;
            }

            switch (this.t) {
                case 1: {
                    v1 = this.getPaddingTop();
                    break;
                }
                case 3: {
                    v1 = this.q - this.getPaddingBottom() - (((int) this.l)) - (((int) this.f)) - this.mAnimationImageView.getMeasuredHeight();
                    break;
                }
                case 2:
                case 4: {
                    v1 = (this.q - (((int) this.l)) - (((int) this.f)) - this.mAnimationImageView.getMeasuredHeight()) / 2;
                    break;
                }
            }

            v0 = (this.p - this.mAnimationImageView.getMeasuredWidth()) / 2;
            this.m = ((((float) this.p)) - this.k) / v7;
            if (this.c == v4) {
                this.mAnimationImageView.layout(v0, v1, this.mAnimationImageView.getMeasuredWidth() + v0, this.mAnimationImageView.getMeasuredWidth() + v1);
                this.n = (((float) v1)) + this.f + (((float) this.mAnimationImageView.getMeasuredHeight())) - this.C.ascent;
                return;
            }

            this.n = (((float) v1)) - this.C.ascent;
            v1 = v1 + (((int) this.l)) + (((int) this.f));
            this.mAnimationImageView.layout(v0, v1, this.mAnimationImageView.getMeasuredWidth() + v0, this.mAnimationImageView.getMeasuredWidth() + v1);
        }
    }

    protected void onMeasure(int arg9, int arg10) {
        int v1;
        int v2;
        int v0 = 0;
        int v7 = -2147483648;
        int v3 = View.MeasureSpec.getMode(arg9);
        int v4 = View.MeasureSpec.getSize(arg9);
        int v5 = View.MeasureSpec.getMode(arg10);
        int v6 = View.MeasureSpec.getSize(arg10);
        if (this.c == 0 || this.c == 1) {
            v1 = ((int) ((((float) (this.getPaddingLeft() + this.mAnimationImageView.getMeasuredWidth()))) + this.f + this.k + (((float) this.getPaddingRight()))));
            v0 = ((int) Math.max(((float) (this.mAnimationImageView.getMeasuredHeight() + this.getPaddingTop() + this.getPaddingBottom())), this.l));
            v2 = v1;
        } else {
            if (this.c != 2 && this.c != 3) {
                v2 = 0;
//                goto label_35;
            } else {
                v1 = ((int) ((((float) (this.getPaddingLeft() + this.getPaddingRight()))) + Math.max(((float) this.mAnimationImageView.getMeasuredWidth()), this.k)));
                v0 = ((int) ((((float) (this.getPaddingBottom() + this.getPaddingTop()))) + this.f + this.l + (((float) this.mAnimationImageView.getMeasuredHeight()))));
                v2 = v1;
            }
        }

//    label_35:
        v1 = v5 == v7 || v5 == 0 ? Math.max(v0, this.s) : Math.max(Math.max(v0, this.s), v6);
        v0 = v3 == v7 || v3 == 0 ? Math.max(v2, this.r) : Math.max(Math.max(v2, this.r), v4);
        this.setMeasuredDimension(v0, v1);
    }

    public void setDiggAnimationView(DiggAnimationView arg1) {
        this.diggAnimationView = arg1;
    }

    public void setDrawablePadding(float arg1) {
        this.f = arg1;
        this.requestLayout();
    }

    public void setSelected(boolean arg2) {
        this.clicked = arg2;
        this.mAnimationImageView.setSelected(arg2);
        this.adaptNightMode(this.b);
    }

    public void setText(String arg3) {
        if (!TextUtils.isEmpty(((CharSequence) arg3))) {
            this.B = arg3;
            this.k = this.e.measureText(arg3);
            this.l = this.C.descent - this.C.ascent;
            this.requestLayout();
        }
    }

    public void setText(int arg2) {
        this.setText(this.mResources.getString(arg2));
    }

    public final void setTextColor$255f295(int arg2) {
        this.i = R.color.ssxinzi4;
        this.j = arg2;
        this.adaptNightMode(this.b);
    }

    public void setTextSize(float arg2) {
        if (this.e != null) {
            this.e.setTextSize(arg2);
            this.C = this.e.getFontMetrics();
            this.requestLayout();
        }
    }
}

